<?php

  // Pour ajouter des équipes
  // auteur : sebastien Mosser
  
class sfEditTeam extends aSubFeature {

   public function __construct() {
      $this->_acl = new AccessControlList();
      $this->_acl->setGranted(7);
      $this->_acl->setGranted(3);
      $this->_acl->setGranted(2);
   }

  protected function run() {
  	
  	$action = PropRetriever::getProp($_POST,"action");
  	
  	if ($action == "edit")
  		return $this->doEdit();
  	else if ($action == "delete")
  		return $this->deleteTeam();
  	else
 			return $this->displayForm();
	}

	private function deleteTeam() {
		//var_dump($_POST);
		$teamId = PropRetriever::getProp($_POST,"teamId");
		$aTeam = Team::read($teamId);
		$name = $aTeam->displayShort();
		$aTeam->delete();
		$page = <<<EOS
<h1> Supression effectuée avec succès </h1>
<br /> <br /> <br />
<p> Vous venez de supprimer l'équipe ${name} </p>

EOS;
		return $page;
	
	}

	private function doEdit() {
		$teamId    = PropRetriever::getProp($_POST,"teamId");
	  $teamName  = PropRetriever::getProp($_POST,"teamName");
		$teamCat   = PropRetriever::getProp($_POST,"teamCat");
		$teamAdmin = PropRetriever::getProp($_POST,"teamAdmin");
		$coachId   = PropRetriever::getProp($_POST,"coachId");
		$d1Id      = PropRetriever::getProp($_POST,"d1Id");
		$d2Id      = PropRetriever::getProp($_POST,"d2Id");
		
		$errors = array();
		
		if ($teamName == "")
			$errors["teamName"] = "Le nom de l'équipe ne peut être vide !";
		if ($teamCat == -1)
			$errors["teamCat"] = 
							"Une équipe doit obligatoirement appartenir a une catégorie !";
		
		if (count($errors) != 0)
			return $this->displayForm($errors);
		
		// On fait les modifications : 
		$aTeam = Team::read($teamId);
		$aTeam->setAdmin($teamAdmin);
		$aTeam->setName(ucwords(strtolower($teamName)));
		$aTeam->setCategory($teamCat);
		$aTeam->setCoach($coachId);
		$aTeam->setDirector1($d1Id);
		$aTeam->setDirector2($d2Id);
		
		$aTeam->update();
		
		$text = $aTeam->displayLong();
		$head = $this->displayForm();
		$page = <<<EOS
<h1> Modification effectuée avec succès </h1>
${text}
EOS;
		return $head . $page;
	}

	private function displayForm($errors = array()) {
		$season = Season::read(Login::seasonChoosen(False));
		$season = $season->displayShort();
		
		// Recupération de l'identifiant d'équipe
		$teamId = PropRetriever::getProp($_POST,"teamId");
		if ($teamId == "")
			$teamId = PropRetriever::getProp($_GET,"id");
		
		$aTeam = Team::read($teamId);
		
		$teamName  = PropRetriever::getProp($_POST,"teamName");
		if ($teamName == "")
			$teamName = $aTeam->getName();
			
		$teamCat   = PropRetriever::getProp($_POST,"teamCat");
		if ($teamCat == "")
			$teamCat = $aTeam->getCategory();
		
		$teamAdmin = PropRetriever::getProp($_POST,"teamAdmin");
		if ($teamAdmin == "") {
			$teamAdmin = $aTeam->getAdmin();
		}
			
		$coachId   = PropRetriever::getProp($_POST,"coachId");
		if ($coachId == "")
			$coachId = $aTeam->getCoach();
		
		$d1Id      = PropRetriever::getProp($_POST,"d1Id");
		if ($d1Id == "")
			$d1Id = $aTeam->getDirector1();
			
		$d2Id      = PropRetriever::getProp($_POST,"d2Id");
		if ($d2Id == "")
			$d2Id = $aTeam->getDirector2();
			
		$DIRECTOR = 5;
		$COACH = 3;
		$UNKNOWN = "<option value=\"-1\"> Inconnu </option>";
		
		$functionsArray = array($COACH, $DIRECTOR);
		$members = Member::getOnFunction($functionsArray,
																		 Login::seasonChoosen(False));
		
		$coachList = Member::getAsList($members[$COACH],$coachId);
		$d1List    = $UNKNOWN . Member::getAsList($members[$DIRECTOR],$d1Id);
		$d2List    = $UNKNOWN . Member::getAsList($members[$DIRECTOR],$d2Id);
		
		$catOptions = "";
		$adminOptions = User::getAsList(1,$teamAdmin);
		$catOptions = TeamCategory::getAsList(Login::seasonChoosen(False),$teamCat);
		
		$style = array();
		$style["teamName"] = ""; $style["teamCat"] = "";
		
		if (($tmp = PropRetriever::getProp($errors,"teamName")) != "")
			$style["teamName"] = "class=\"error\"";
			
		if (($tmp = PropRetriever::getProp($errors,"teamCat")) != "")
			$style["teamCat"] = "class=\"error\"";
		
		$errorsText = "";
		foreach ($errors as $k => $v) {
			$errorsText .= "<li> <span class=\"error\"> $v </span> </li>";
		}
		if ($errorsText != "") {
			$tmp = "<h2> Des erreurs sont survenues ... </h2>";
			$tmp .= "<ul> " . $errorsText . " </ul>";
			$errorsText = $tmp;
		}
		
		$page = <<<EOS
<h1> Modification d'une équipe pour la saison [ {$season} ] </h1>

<div align="center">
<br /> <br />

<form method="post" accept-charset="utf-8" name="editTeam"
			action="?edit" >	

<table> 
			
<input type="hidden" name="action" value="edit" />
<input type="hidden" name="teamId" value="${teamId}" />
<tr> 
<td align="center">

	<fieldset>
		<legend> Informations Administratives </legend>
		<br />
		
		
		<label> 
			<span ${style["teamName"]}>Nom : </span>
			<input type="text" name="teamName" class="edit" value="${teamName}"/>
			&nbsp; &nbsp;
		</label>
		<br />		
		
		<label> 
			<span ${style["teamCat"]}>Categorie : </span>
			<select name="teamCat" class="edit" />
			${catOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> Administrateur : </span>
			<select name="teamAdmin" class="edit" />
			${adminOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		

		<br />
	</fieldset>
	
</td>

<td> &nbsp;&nbsp;&nbsp;&nbsp; </td>

<td align="center">
  <fieldset>
		<legend> Staff de l'équipe </legend>
		<br />
		
		<label> 
			<span>Entraineur : </span>
			<select name="coachId" class="edit" />
			${coachList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Dirigeant [1] : </span>
			<select name="d1Id" class="edit" />
			${d1List}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Dirigeant [2] : </span>
			<select name="d2Id" class="edit" />
			${d2List}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />

		<br /> 
	</fieldset>
</td>
</tr>

<tr>
  <td align="center">
  	<br />
    <input type="submit" value="Enregistrer les modifications" class="edit" />
  </td>
  </form>
  
  <td> &nbsp;&nbsp;&nbsp;&nbsp; </td>
  			
  <td align="center">
  <form method="post" accept-charset="utf-8" name="delTeam"
			action="?del" >
    <input type="hidden" name="action" value="delete" />
    <input type="hidden" name="teamId" value="${teamId}" />
     <br />
    <input type="submit" value="Supprimer l'équipe" class="edit" onclick="return window.confirm('Attention, cette action est irréversible ... \\nVeuillez confirmer la suppression en cliquant sur \'OK\'.')" />
  </form>
  </td>
  
</tr>
</table>
</div>

${errorsText}
EOS;
		return $page;
	}


}
?>